﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ScarEngine
{

    public class GraphicsManager
    {
        public GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public List<DrawableComponent> DrawList;

        public GraphicsManager()
        {
            DrawList = new List<DrawableComponent>();
            graphics = new GraphicsDeviceManager(Game1.Game);
            
        }

        public void PreRun() // run before the first update or draw
        {
            SetResolution(800, 600);
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
        }

        #region Component

        public void RegisterComponent(DrawableComponent c)
        {
            if (!DrawList.Contains(c))
            {
                DrawList.Add(c);
            }
        }

        public void UnRegisterComponent(DrawableComponent c)
        {
            if (DrawList.Contains(c))
            {
                DrawList.Remove(c);
            }
        }

        #endregion

        #region Clear
        public void ClearDrawList()
        {
            DrawList.Clear();
        }
        #endregion

        #region Draw Function
        public void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black); //Clear the screen every cycle

            foreach (DrawableComponent Drawable in DrawList)
            {
                spriteBatch.Begin();
                Drawable.Draw(gameTime, spriteBatch);
                spriteBatch.End();
            }


        }
        #endregion

        #region Additional Methods

        public void SetResolution(float Width, float Height)
        {
            graphics.PreferredBackBufferWidth = (int)Math.Abs((decimal)Width);
            graphics.PreferredBackBufferHeight = (int)Math.Abs((decimal)Height);
            graphics.ApplyChanges();
        }


        #endregion

    }
}